package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.android.gpuimage.AdjustGPUImageView;
import com.android.graffiti.GraffitoView;
import com.android.graffiti.IndicativeHorizontalScrollView;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.bean.BitmapBean;
import com.photoeditor.demo.model.image.mirror.MirrorTempletConstant;
import com.photoeditor.demo.model.image.util.ImageHelper;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.activity.GalleryActivity;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.RotationImageView;
import com.photoeditor.demo.ui.widget.beauty.BigEyesView;
import com.photoeditor.demo.ui.widget.beauty.BreastView;
import com.photoeditor.demo.ui.widget.beauty.RepairView;
import com.photoeditor.demo.ui.widget.beauty.TailImageView;
import com.photoeditor.demo.ui.widget.collage.CollageRelativeLayout;
import com.photoeditor.demo.ui.widget.image.AnimationCropImageView;
import com.photoeditor.demo.ui.widget.image.brush.BrushView;
import com.photoeditor.demo.ui.widget.image.brush.MopiBrushView;
import com.photoeditor.demo.ui.widget.image.brush.WhitenBrushView;
import com.photoeditor.demo.ui.widget.image.compose.CanvasEditTextView;
import com.photoeditor.demo.ui.widget.image.emoji.CanvasEditEmojiView;
import com.photoeditor.demo.ui.widget.image.eraser.ImageEraserView;
import com.photoeditor.demo.ui.widget.image.hair.Color_view;
import com.photoeditor.demo.ui.widget.image.lip.LipView;
import com.photoeditor.demo.ui.widget.image.mixer.ImageMixerView;
import com.photoeditor.demo.ui.widget.image.watermark.Watermark;
import com.photoeditor.demo.ui.widget.image.watermark.WatermarkManager;
import com.photoeditor.demo.util.CameraDrawable;
import com.photoeditor.demo.util.ImageRectUtils;

import java.util.ArrayList;

import static com.photoeditor.demo.statistics.StatisticsConstant.MAIN_BTN_EDIT;
import static com.photoeditor.demo.statistics.StatisticsConstant.UMENG_CUSTOM_EVENT_EDITOR;
import static com.photoeditor.demo.statistics.StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_CLICK;
import static com.photoeditor.demo.statistics.StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SHOW;
import static com.photoeditor.demo.statistics.StatisticsConstant.UMENG_CUSTOM_KEY_FUNCTION;
import static com.photoeditor.demo.statistics.StatisticsConstant.UMENG_CUSTOM_UNDER_LINE;
import static com.photoeditor.demo.ui.activity.edit.EditorImageActivity.REQUEST_CODE_SELECT_PIC_2_MIXER;

/**
 * @author lyzirving
 * time        2018/8/9
 * email       lyzirving@sina.com
 * information
 */

public class EditOperationView extends LinearLayout implements View.OnClickListener {

    public static final int TYPE_EDIT = 3;

    private int mCurId;

    private IndicativeHorizontalScrollView mScrollBar;
    private LinearLayout mRoot;
    private CustomTabButton mCropBtn;
    private CustomTabButton mAdjustBtn;
    private CustomTabButton mWatermarkBtn;
    private CustomTabButton mTextBtn;
    private CustomTabButton mDoogleBtn;
    private CustomTabButton mMixerBtn;
    private CustomTabButton mMirrorBtn;
    private CustomTabButton mBlurBtn;
    private CustomTabButton mRotateBtn;

    private BitmapBean mImageBean;
    private CameraDrawable mSrcBitmap;
    private EditorImageActivity mActivity;
    private AnimationCropImageView mCropImageView;
    private AdjustGPUImageView mAdjustGPUImageView;
    private WatermarkManager mWatermarkManager;
    private CanvasEditTextView mCanvasEditTextView;
    private GraffitoView mGraffitoView;
    private ImageMixerView mImageMixerView;
    private ImageEraserView mImageEraserView;
    private CollageRelativeLayout mMirrorView;
    private RotationImageView mRotationImageView;
    private CanvasEditEmojiView mCanvasEditEmojiView;
    private Color_view mHairColorView;
    private WhitenBrushView mWhitenBrushView;
    private BrushView mWhitenTeethView;
    private LipView mLipView;
    private BigEyesView mBigEyesView;
    private RepairView mRepairView;
    private MopiBrushView mMopiBrushView;
    private BreastView mBreastView;
    private BreastView mButtocksView;
    private TailImageView mTailView;

    private float mHorizontalItemWidth = -1;
    private int mBarItemCount = -1;
    private float mFraction;
    private float mHorizontalBarScrollX;

    public EditOperationView(Context context) {
        this(context, null);
    }

    public EditOperationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditOperationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_eidt_bar, this, true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mScrollBar = findViewById(R.id.scroll_bar);
        mRoot = findViewById(R.id.main);
        mCropBtn = findViewById(R.id.crop);
        mAdjustBtn = findViewById(R.id.adjust);
        mWatermarkBtn = findViewById(R.id.watermark);
        mTextBtn = findViewById(R.id.addtext);
        mDoogleBtn = findViewById(R.id.doodle);
        mMixerBtn = findViewById(R.id.op_mixer);
        mMirrorBtn = findViewById(R.id.mirror);
        mBlurBtn = findViewById(R.id.tilt_shift);
        mRotateBtn = findViewById(R.id.rotate);

        mCropBtn.setOnClickListener(this);
        mAdjustBtn.setOnClickListener(this);
        mWatermarkBtn.setOnClickListener(this);
        mTextBtn.setOnClickListener(this);
        mDoogleBtn.setOnClickListener(this);
        mMixerBtn.setOnClickListener(this);
        mMirrorBtn.setOnClickListener(this);
        mBlurBtn.setOnClickListener(this);
        mRotateBtn.setOnClickListener(this);

        mScrollBar.setEditOperationView(this);
        mScrollBar.setType(TYPE_EDIT);
        mScrollBar.setOnHorScrollChangeListener(new IndicativeHorizontalScrollView.HorizontalScrollChangeListener() {
            @Override
            public void onScroll(int l, int oldl) {
                if (mHorizontalItemWidth <= 0) {
                    getItemWidth();
                    return;
                }
                if (mBarItemCount <= 0) {
                    getBarItemCount();
                }
                mHorizontalBarScrollX = l;
            }
        });
    }

    private void changeContentStatus(int id) {
        switch (id) {
            case R.id.crop:
                mActivity.showInsideBottomBarWithName(R.string.image_edit_crop, View.NO_ID);
                mActivity.setConfirmEnable(true);
                mActivity.getCropBarView().setVisibility(View.VISIBLE);
                mAdjustGPUImageView.getGPUImage().deleteImage();
                if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
                    mAdjustGPUImageView.setImage(mSrcBitmap.getBitmap());
                }
                statisticEditItemClick(0);
                break;
            case R.id.adjust:
                mActivity.showInsideBottomBarWithName(R.string.image_edit_adjust, View.NO_ID);
                mActivity.setConfirmEnable(false);
                mActivity.getAdjustBarView().setVisibility(View.VISIBLE);
                mAdjustGPUImageView.getGPUImage().deleteImage();
                if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
                    mAdjustGPUImageView.setImage(mSrcBitmap.getBitmap());
                }
                statisticEditItemClick(1);
                break;
            case R.id.watermark:
                mActivity.showInsideBottomBarWithName(R.string.watermark, View.NO_ID);
                mActivity.setConfirmEnable(true);
                mActivity.getWatermarkBarView().setVisibility(View.VISIBLE);
                mAdjustGPUImageView.getGPUImage().deleteImage();
                if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
                    mAdjustGPUImageView.setImage(mSrcBitmap.getBitmap());
                }
                statisticEditItemClick(2);
                break;
            case R.id.addtext:
                mActivity.showInsideBottomBarWithName(R.string.image_preview_text, View.NO_ID);
                mActivity.setConfirmEnable(false);
                mActivity.getAddTextBarView().setVisibility(View.VISIBLE);
                mCanvasEditTextView.setImageDrawable(mSrcBitmap);
                statisticEditItemClick(3);
                break;
            case R.id.doodle:
                mActivity.showInsideBottomBarWithName(R.string.image_edit_doodle, View.NO_ID);
                mActivity.setConfirmEnable(false);
                mActivity.getDoodleBarView().setVisibility(View.VISIBLE);
                if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
                    mGraffitoView.setExtraBitmap(mSrcBitmap.getBitmap(), true);
                }
                mGraffitoView.reset(mActivity.getDoodleBarView().getMosaicType());
                statisticEditItemClick(4);
                break;
            case R.id.op_mixer:
                GalleryActivity.startGalleryChangeCollageImages(mActivity, REQUEST_CODE_SELECT_PIC_2_MIXER);
                mActivity.showInsideBottomBarWithName(R.string.image_edit_mixer, R.drawable.ic_tool_help);
                mActivity.setConfirmEnable(true);
                statisticEditItemClick(5);
                break;
            case R.id.mirror:
                mActivity.showInsideBottomBarWithName(R.string.image_edit_other_mirror, View.NO_ID);
                mActivity.setConfirmEnable(true);
                if (mActivity.getMirrorBarView() != null && mActivity.getMirrorBarView().getBaseBitmap() != mSrcBitmap.getBitmap()) {//说明两边不一致  需要刷新
                    mActivity.getMirrorBarView().setBaseBitmap(mSrcBitmap.getBitmap(), mImageBean);
                }
                mActivity.getMirrorBarView().setVisibility(View.VISIBLE);
                if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
                    mMirrorView.setSourceBitmaps(mSrcBitmap.getBitmap());
                }
                ArrayList<BitmapBean> list = new ArrayList<>(1);
                list.add(mImageBean);
                mMirrorView.setSourceBitmapBeans(list);
                mMirrorView.setTemplet(MirrorTempletConstant.mirrorTemplet.get(0));
                statisticEditItemClick(6);
                break;
            case R.id.tilt_shift:
                mActivity.showInsideBottomBarWithName(R.string.image_edit_adjust_tiltshift, View.NO_ID);
                mActivity.setConfirmEnable(true);
                mActivity.getTiltShiftBarView().setVisibility(View.VISIBLE);
                mAdjustGPUImageView.getGPUImage().deleteImage();
                if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
                    mAdjustGPUImageView.setImage(mSrcBitmap.getBitmap());
                }
                statisticEditItemClick(7);
                break;
            case R.id.rotate:
                mActivity.showInsideBottomBarWithName(R.string.image_edit_rotate, View.NO_ID);
                mActivity.setConfirmEnable(false);
                mActivity.getRotatebarView().setVisibility(View.VISIBLE);
                mActivity.getRotatebarView().restore();
                mRotationImageView.setImageDrawable(mSrcBitmap);
                statisticEditItemClick(8);
                break;
            default:
        }
    }

    private void doShowOperationAnimation(int id) {
        switch (id) {
            case R.id.crop:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.adjust:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.watermark:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.addtext:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.doodle:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.op_mixer:
                //在onActivityResult中处理mixer的回调
                break;
            case R.id.mirror:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.tilt_shift:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            case R.id.rotate:
                mActivity.hideTopView();
                mActivity.doShowOperationBarAnim();
                break;
            default:
        }
    }

    private void initBtnState() {
        mCropBtn.setThemeImageRes(R.drawable.ic_tool_crop, View.NO_ID);
        mAdjustBtn.setThemeImageRes(R.drawable.ic_edit_adjust, View.NO_ID);
        mWatermarkBtn.setThemeImageRes(R.drawable.ic_tool_watermark, View.NO_ID);
        mTextBtn.setThemeImageRes(R.drawable.ic_tool_text, View.NO_ID);
        mDoogleBtn.setThemeImageRes(R.drawable.ic_tool_doodle, View.NO_ID);
        mMixerBtn.setThemeImageRes(R.drawable.ic_edit_mixer, View.NO_ID);
        mMirrorBtn.setThemeImageRes(R.drawable.ic_tool_mirror, View.NO_ID);
        mBlurBtn.setThemeImageRes(R.drawable.ic_tool_blur, View.NO_ID);
        mRotateBtn.setThemeImageRes(R.drawable.ic_tool_rotate, View.NO_ID);
    }

    public void scrollToStart() {
        if (mScrollBar != null) {
            mScrollBar.scrollTo(0, 0);
        }
        mHorizontalBarScrollX = 0;
    }

    @Override
    public void onClick(View v) {
        mCurId = v.getId();
        changeContentStatus(mCurId);
        doShowOperationAnimation(mCurId);
    }

    public void clickWatermark() {
        onClick(mWatermarkBtn);
    }

    public void init() {
        initBtnState();
    }

    public void setActivity(EditorImageActivity activity) {
        mActivity = activity;
    }

    public void setImageBean(BitmapBean bean) {
        mImageBean = bean;
    }

    public void setCropImageView(AnimationCropImageView cropImageView) {
        mCropImageView = cropImageView;
    }

    public void setAdjustGPUImageView(AdjustGPUImageView view) {
        mAdjustGPUImageView = view;
    }

    public void setSrcBitmap(CameraDrawable bitmap) {
        mSrcBitmap = bitmap;
    }

    public void setWatermarkManager(WatermarkManager manager) {
        mWatermarkManager = manager;
    }

    public void setCanvasEditTextView(CanvasEditTextView view) {
        mCanvasEditTextView = view;
    }

    public void setGraffitoView(GraffitoView view) {
        mGraffitoView = view;
    }

    public void setImageMixerView(ImageMixerView view) {
        mImageMixerView = view;
    }

    public void setImageEraserView(ImageEraserView view) {
        mImageEraserView = view;
    }

    public void setMirrorView(CollageRelativeLayout view) {
        mMirrorView = view;
    }

    public void setRotationImageView(RotationImageView view) {
        mRotationImageView = view;
    }

    public void setCanvasEditEmojiView(CanvasEditEmojiView view) {
        mCanvasEditEmojiView = view;
    }

    public void setHairColorView(Color_view view) {
        mHairColorView = view;
    }

    public void setWhitenBrushView(WhitenBrushView view) {
        mWhitenBrushView = view;
    }

    public void setWhitenTeethView(BrushView view) {
        mWhitenTeethView = view;
    }

    public void setLipView(LipView view) {
        mLipView = view;
    }

    public void setBigEyesView(BigEyesView view) {
        mBigEyesView = view;
    }

    public void setRepairView(RepairView view) {
        mRepairView = view;
    }

    public void setMopiBrushView(MopiBrushView view) {
        mMopiBrushView = view;
    }

    public void setBreastView(BreastView view) {
        mBreastView = view;
    }

    public void setButtocksView(BreastView view) {
        mButtocksView = view;
    }

    public void setTailView(TailImageView view) {
        mTailView = view;
    }

    public void updateBottomTabSelectStatus() {
        switch (mCurId) {
            case R.id.crop:
                mCropImageView.setVisibility(View.VISIBLE);
                mCropImageView.setCropOverlayViewVisibility(View.VISIBLE);
                mAdjustGPUImageView.setVisibility(GONE);
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.adjust:
                mCropImageView.setVisibility(View.VISIBLE);
                mAdjustGPUImageView.setVisibility(VISIBLE);
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.watermark:
                mCropImageView.setVisibility(View.VISIBLE);
                mAdjustGPUImageView.setVisibility(VISIBLE);
                mActivity.setWaterMarkPosition(ImageRectUtils.getViewRect((View) mCropImageView.getParent()),
                        mActivity.getDrawableRect(mCropImageView));
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.addtext:
                mCanvasEditTextView.setVisibility(VISIBLE);
                mCropImageView.setVisibility(View.GONE);
                mAdjustGPUImageView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.doodle:
                mGraffitoView.setVisibility(VISIBLE);
                mCropImageView.setVisibility(View.GONE);
                mAdjustGPUImageView.setVisibility(GONE);
                mCanvasEditTextView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.op_mixer:
                mCropImageView.setVisibility(View.GONE);
                mAdjustGPUImageView.setVisibility(GONE);
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.VISIBLE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.mirror:
                mMirrorView.setVisibility(View.VISIBLE);
                mCropImageView.setVisibility(View.GONE);
                mAdjustGPUImageView.setVisibility(GONE);
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.tilt_shift:
                mCropImageView.setVisibility(View.GONE);
                mAdjustGPUImageView.setVisibility(VISIBLE);
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mRotationImageView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            case R.id.rotate:
                mRotationImageView.setVisibility(View.VISIBLE);
                mCropImageView.setVisibility(View.GONE);
                mAdjustGPUImageView.setVisibility(GONE);
                mCanvasEditTextView.setVisibility(GONE);
                mGraffitoView.setVisibility(GONE);
                mImageEraserView.setVisibility(View.GONE);
                mImageMixerView.setVisibility(View.GONE);
                mMirrorView.setVisibility(View.GONE);
                mCanvasEditEmojiView.setVisibility(GONE);
                mHairColorView.setVisibility(GONE);
                mWhitenBrushView.setVisibility(GONE);
                mWhitenTeethView.setVisibility(GONE);
                mLipView.setVisibility(GONE);
                mBigEyesView.setVisibility(GONE);
                mRepairView.setVisibility(GONE);
                mMopiBrushView.setVisibility(GONE);
                mBreastView.setVisibility(GONE);
                mButtocksView.setVisibility(GONE);
                mTailView.setVisibility(GONE);
                break;
            default:
                break;
        }
    }

    public boolean onCancelClick() {
        boolean result = true;
        result = mActivity.editOperationBarCanceled(mCurId);
        if (result) {
            mCurId = View.NO_ID;
        }
        return result;
    }

    public boolean isSelectWatermark() {
        return mCurId == R.id.watermark;
    }

    public boolean onConfirmClick() {
        boolean result = true;
        result = doConfirmWork();
        if (result) {
            resetContents();
            refreshBitmap();
            doAnimation();
            mCurId = View.NO_ID;
        }
        return result;
    }

    public void onProgressChanged(int progress) {
        switch (mCurId) {
            case R.id.adjust:
                mActivity.getAdjustBarView().onProgressChange(progress);
                break;
            case R.id.tilt_shift:
                mActivity.getTiltShiftBarView().onProgressChange(progress);
                break;
            default:
        }
    }

    public int getCurSelectBtnId() {
        return mCurId;
    }

    private boolean doConfirmWork() {
        boolean result = true;
        Bitmap bitmap = null;
        switch (mCurId) {
            case R.id.crop:
                mActivity.resetGPUImageView();
                bitmap = mCropImageView.getCroppedImage();
                if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                    bitmap = ImageHelper.getScaleBitmap(bitmap);
                    CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                    mCropImageView.setImageDrawable(cd);
                    mSrcBitmap = cd;
                    mActivity.updateSrcBitmap(cd);
                } else {
                    mCropImageView.setImageDrawable(mSrcBitmap);
                }
                mActivity.setNeedSave(true);
                break;
            case R.id.adjust:
                //只有在底部显示adjust的情况下才能最终confirm
                if (mActivity.getAdjustBarView().onConfirmClick()) {
                    mActivity.showInsideBottomBarWithName(R.string.image_edit_adjust, View.NO_ID);
                    mActivity.setConfirmEnable(mActivity.getAdjustBarView().isModified());
                    result = false;
                } else {
                    bitmap = mAdjustGPUImageView.getCurrentBitmap();
                    mActivity.getAdjustBarView().cancelFilter();
                    if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                        CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                        mCropImageView.setImageDrawable(cd);
                        mSrcBitmap = cd;
                        mActivity.updateSrcBitmap(cd);
                    } else {
                        mCropImageView.setImageDrawable(mSrcBitmap);
                    }
                    mActivity.setNeedSave(true);
                }
                break;
            case R.id.watermark:
                Watermark watermark = mWatermarkManager.getCurrentWatermark();
                if (mWatermarkManager.isPlaceholderWatermark(watermark)) {//没有水印
                    mActivity.getWatermarkIV().setImageBitmap(null);
                } else {
                    bitmap = mWatermarkManager.getWatermarkBitmap(watermark);
                    if (bitmap != null) {
                        mActivity.getWatermarkIV().setImageBitmap(bitmap);
                    }
                }
                mActivity.refreshWaterMark();
                mWatermarkManager.saveLastUseWatermark();
                mActivity.setNeedSave(true);
                break;
            case R.id.addtext:
                mActivity.getAddTextBarView();
                bitmap = mCanvasEditTextView.getDstBitmap();
                mCanvasEditTextView.reset();
                if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                    CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                    mCropImageView.setImageDrawable(cd);
                    mCanvasEditTextView.setImageBitmap(bitmap);
                    mSrcBitmap = cd;
                    mActivity.updateSrcBitmap(cd);
                } else {
                    mCropImageView.setImageDrawable(mSrcBitmap);
                }
                mActivity.setNeedSave(true);
                break;
            case R.id.doodle:
                bitmap = mGraffitoView.getContentBitmap();
                if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                    CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                    mCropImageView.setImageDrawable(cd);
                    mSrcBitmap = cd;
                    mActivity.updateSrcBitmap(cd);
                } else {
                    mCropImageView.setImageDrawable(mSrcBitmap);
                }
                mGraffitoView.reset();
                mActivity.setNeedSave(true);
                break;
            case R.id.op_mixer:
                mActivity.getMixerBar();
                boolean isConfirm = mActivity.getMixerBar().onConfirmClick();
                if (isConfirm) {
                    if (mActivity.openEraseForMixer(false, true)) {
                        result = false;
                        break;
                    }
                    bitmap = mImageMixerView.getDestBitmap();
                    if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                        CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                        mCropImageView.setImageDrawable(cd);
                        mSrcBitmap = cd;
                        mActivity.updateSrcBitmap(cd);
                    } else {
                        mCropImageView.setImageDrawable(mSrcBitmap);
                    }
                    mImageMixerView.reset();
                    mActivity.getMixerBar().reset();
                    mActivity.resetGPUImageView();
                    mActivity.setNeedSave(true);
                } else {
                    result = false;
                    ;
                }
                break;
            case R.id.mirror:
                mActivity.resetGPUImageView();
                bitmap = mMirrorView.getCollageBitmap();
                if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                    CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                    mCropImageView.setImageDrawable(cd);
                    mSrcBitmap = cd;
                    mActivity.updateSrcBitmap(cd);
                } else {
                    mCropImageView.setImageDrawable(mSrcBitmap);
                }
                mActivity.getMirrorBarView().reset();
                mActivity.setNeedSave(true);
                break;
            case R.id.tilt_shift:
                bitmap = mAdjustGPUImageView.getCurrentBitmap();
                if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                    CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                    mCropImageView.setImageDrawable(cd);
                    mSrcBitmap = cd;
                    mActivity.updateSrcBitmap(cd);
                } else {
                    mCropImageView.setImageDrawable(mSrcBitmap);
                }
                mActivity.getTiltShiftBarView().cancel();
                mActivity.setNeedSave(true);
                break;
            case R.id.rotate:
                mActivity.resetGPUImageView();
                bitmap = mRotationImageView.getCurrentBitmap();
                mActivity.getRotatebarView().restore();
                if (bitmap != null && bitmap != mSrcBitmap.getBitmap()) {
                    CameraDrawable cd = new CameraDrawable(getResources(), bitmap);
                    mCropImageView.setImageDrawable(cd);
                    mRotationImageView.setImageDrawable(cd);
                    mSrcBitmap = cd;
                    mActivity.updateSrcBitmap(cd);
                } else {
                    mCropImageView.setImageDrawable(mSrcBitmap);
                }
                mActivity.setNeedSave(true);
                break;
            default:
        }
        return result;
    }

    private void refreshBitmap() {
        mAdjustGPUImageView.getGPUImage().deleteImage();
        if (mSrcBitmap != null && mSrcBitmap.getBitmap() != null && !mSrcBitmap.getBitmap().isRecycled()) {
            mAdjustGPUImageView.setImage(mSrcBitmap.getBitmap());
        }
    }

    private void doAnimation() {
        mActivity.editOperationBarConfirmed(mCurId);
        mActivity.showTopView();
        mActivity.doHideOperationBarAnim();
        mActivity.showOperationsViewByTabId(R.id.edit);
    }

    private void resetContents() {
        mAdjustGPUImageView.setVisibility(View.VISIBLE);
        mCropImageView.setVisibility(View.VISIBLE);
        mCanvasEditTextView.setVisibility(View.GONE);
        mGraffitoView.setVisibility(View.GONE);
        mImageEraserView.setVisibility(View.GONE);
        mImageMixerView.setVisibility(View.GONE);
        mMirrorView.setVisibility(View.GONE);
        mRotationImageView.setVisibility(View.GONE);
    }

    public void updateSrcBitmap(CameraDrawable bitmap) {
        mSrcBitmap = bitmap;
    }

    public void setSelectedId(int id) {
        mCurId = id;
    }

    public void onMixerImageSelect() {
        mCurId = R.id.op_mixer;
        mActivity.showMixerInEdit();
        mActivity.hideTopView();
        mActivity.doShowOperationBarAnim();
    }

    public boolean isEditSelectMixer() {
        return mCurId == R.id.op_mixer;
    }

    public void getItemWidth() {
        if (mCropBtn.getVisibility() == VISIBLE) {
            mHorizontalItemWidth = mCropBtn.getMeasuredWidth();
        }
    }

    public void getBarItemCount() {
        if (mHorizontalItemWidth <= 0) {
            mBarItemCount = -1;
            return;
        }
        float countF = DeviceUtils.getScreenWidthPx() * 1f / mHorizontalItemWidth;
        mBarItemCount = (int) (DeviceUtils.getScreenWidthPx() * 1f / mHorizontalItemWidth);
        mFraction = countF - mBarItemCount;
    }

    public void statisticItemShow(float scrollX) {
        if (mHorizontalItemWidth <= 0 || mBarItemCount <= 0) {
            return;
        }
        float scrollCountF = scrollX / mHorizontalItemWidth;
        if ((int) scrollCountF != 0) {
            scrollCountF += mFraction;
        }
        int tmpTotalCount = 0;
        int scrollCountInt = (int) scrollCountF;
        if (scrollCountF - scrollCountInt > 0.5f) {
            if (scrollCountF - scrollCountInt > 0.6f) {
                tmpTotalCount = mBarItemCount + 1;
            } else {
                tmpTotalCount = mBarItemCount;
            }
            scrollCountInt++;
        } else if (scrollCountF - scrollCountInt < 0.05f && scrollCountF - scrollCountInt > 0) {
            tmpTotalCount = mBarItemCount + 1;
        } else {
            tmpTotalCount = mBarItemCount;
        }
        int lastIndex = mBarItemCount + scrollCountInt - 1;
        if (lastIndex >= mRoot.getChildCount()) {
            lastIndex = mRoot.getChildCount() - 1;
        }
        int startIndex = lastIndex - tmpTotalCount + 1;
        if (startIndex < 0) {
            startIndex = 0;
        }
        for (int i = startIndex; i <= lastIndex; i++) {
            statisticEditItemShow(i);
        }
    }

    private void statisticEditItemShow(int index) {
        switch (index) {
            case 0:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "crop" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 1:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "adjust" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 2:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "watermark" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 3:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "text" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 4:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "doodle" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 5:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mixer" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 6:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mirror" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 7:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "blur" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            case 8:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "rotate" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
                break;
            default:
                break;
        }
    }

    private void statisticEditItemClick(int index) {
        switch (index) {
            case 0:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "crop" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("crop");
                break;
            case 1:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "adjust" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("adjust");
                break;
            case 2:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "watermark" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("watermark");
                break;
            case 3:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "text" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("text");
                break;
            case 4:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "doodle" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("doodle");
                break;
            case 5:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mixer" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("mixer");
                break;
            case 6:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mirror" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("mirror");
                break;
            case 7:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "blur" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("blur");
                break;
            case 8:
                StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
                        MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "rotate" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
                SPDataManager.setSaveFunctionName("rotate");
                break;
            default:
                break;
        }
    }

    public float getHorizontalBarScrollX() {
        return mHorizontalBarScrollX;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mScrollBar != null) {
            mScrollBar.cancelEditStatisticTask();
        }
    }

}
